package com.ygxsk.carhome.ui.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.listener.DoubleClickListener;
import com.ygxsk.carhome.response.MyOrderBean;
import com.ygxsk.carhome.response.ShopCartBean;
import com.ygxsk.carhome.utils.CommUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by itchenqi on 2018/2/26 0026.
 * description：订单大列表和订单详情内部外侧的大列表适配器，只不过订单详情内部是一条数据的列表项。
 */
public class OrderDetailAdapter extends BaseQuickAdapter<MyOrderBean, BaseViewHolder> {

    public OrderDetailAdapter(int layoutResId, @Nullable List<MyOrderBean> data) {
        super(layoutResId, data);
        //mSession = Session.get(mContext);
    }

    @Override
    protected void convert(BaseViewHolder helper, final MyOrderBean item) {

        if (item != null) {

            //填充数据内容。。。
            String orderNumber = item.getOrderNo();
            String stateName = item.getOrderStateInfo();//状态的文本信息，另外还有状态的数字标识。

            ArrayList<ShopCartBean> detailList = item.getOrderDetails();

            int count = detailList.size();

            //String yangjiao = mSession.getCurrencySymbol();
            //2018年04月24日添加优惠券所以这个该使用实际支付金额内容。
            BigDecimal amount = item.getTotalAmount();
            //BigDecimal amount = item.getFinalAmount();

            //优惠券抵扣
            BigDecimal discountAmount = item.getDiscountAmount();

            //实际付款金额
            BigDecimal finalAmount = item.getFinalAmount();

            RecyclerView recyclerView = (RecyclerView) helper.getView(R.id.recyclerViewinorder);

            //填充列表的适配器内容
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

            //filldata
            helper.setText(R.id.textViewTime, "订单编号：" + (TextUtils.isEmpty(orderNumber) ? "" : orderNumber));//订单编号内容
            helper.setText(R.id.textViewState, TextUtils.isEmpty(stateName) ? "" : stateName);//订单状态

            helper.setText(R.id.textNubmerTotal, "共" + count + "件商品");//订单购买数量。


            //添加横线内容 白色横线，目前仅仅有一条商品可以不添加。
            //recyclerView.addItemDecoration(new DividerItemDecorationInAdapter(mContext));

            //OrderInsideAdapter和订单列表通用的内部小列表
            OrderInsideAdapter accountPayAdapter = new OrderInsideAdapter(R.layout.orderinsideadapter, detailList);
            recyclerView.setAdapter(accountPayAdapter);//设置适配器

            //2018年02月27日。
            //这里的recyclerview抢占了焦点，导致点击列表无法进入商品详情。
            //将点击事件添加到内部列表适配器上用于跳转到商品详情
            accountPayAdapter.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                    if (!DoubleClickListener.isFastDoubleClick()) {

                        ShopCartBean shopCartBean = (ShopCartBean) adapter.getItem(position);

                        //通用的跳转到商品详情的界面内容。
                        CommUtils.intentToWebDetailGoods(shopCartBean);


                    }
                }
            });

            accountPayAdapter.notifyDataSetChanged();

            //商品总额===start====如果是农品奥莱的就是会员产品的话就不要底部总额度等============================================================================
            LinearLayout linarLayoutBottomOleGone = (LinearLayout) helper.getView(R.id.linarlayoutbottomaolaigone);

            //若isVipOrder=1;是会员商品 那末不显示商品价格和下边的总价格
            if (item.getIsVipOrder() == 1) {

                //LogUtils.d("itchen--->订单是--会员商品");

                linarLayoutBottomOleGone.setVisibility(View.GONE);

            } else {

                //LogUtils.d("itchen--->订单是--普通的商品");

                linarLayoutBottomOleGone.setVisibility(View.VISIBLE);

                if (amount != null) {
                    helper.setText(R.id.textPriceTotal, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + //
                            CommUtils.formatComma2BigDecimal(amount).toString());//商品总额：总共的价钱
                } else {
                    helper.setText(R.id.textPriceTotal, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + "0.00");
                }
                //====================================================================================

                //优惠金额==================================================================================
                if (discountAmount != null) {
                    helper.setText(R.id.textpricecoupon, "- " + MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) +//
                            CommUtils.formatComma2BigDecimal(discountAmount).toString());
                } else {
                    helper.setText(R.id.textpricecoupon, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + "0.00");
                }
                //==============================================================================================

                //itchen 将来放开
                //如果包含运费【送货上门】，那么就展示运费，如果结算时候选择的是【上门自提】，就不包含运费，运费隐藏。
                RelativeLayout layoutcontentfreight = (RelativeLayout) helper.getView(R.id.layoutcontentfreight);

                layoutcontentfreight.setVisibility(View.VISIBLE);

                if (item.getFreight() != null) {

                    helper.setText(R.id.textpricefreight, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) +//
                            CommUtils.formatComma2BigDecimal(item.getFreight()).toString());
                } else {

                    helper.setText(R.id.textpricefreight, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + "0.00");

                }

                //测试
                //int flag = 1;
                //BigDecimal freight =CommUtils.valueOf(5.00);

                //开始说店铺自提不展示运费，送货上门 展示运费，后来说都显示
                //sendType（配送方式 1自提、2送货上门）2、receiptCode（1、当日达；2、次日达）3、receiptAddress（收货地址）
                /*if (item.getSendType()==1) {//上门自提  就不展示运费，运费也不用取出

                    layoutcontentfreight.setVisibility(View.GONE);

                }else if(item.getSendType()==2){//送货上门 需要展示出运费

                    layoutcontentfreight.setVisibility(View.VISIBLE);

                    if (item.getFreight() != null) {

                        helper.setText(R.id.textpricefreight, SPUtils.getInstance().getString(ConstantApi.P_CURRENCYSYMBOL) +//
                                CommUtils.formatComma2BigDecimal(item.getFreight()).toString());
                    } else {

                        helper.setText(R.id.textpricefreight, SPUtils.getInstance().getString(ConstantApi.P_CURRENCYSYMBOL) + "0.00");

                    }

                }*/

                //实际付款总金额==================================================================================
                if (finalAmount != null) {
                    helper.setText(R.id.textpricefinal, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) +//
                            CommUtils.formatComma2BigDecimal(finalAmount).toString());
                } else {
                    helper.setText(R.id.textpricefinal, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + "0.00");
                }

            }

            //========商品总额 end=========================================================================================================================

        }

    }

}
